package com.zheng.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.zheng.mall.mapper.OmsOrderReturnReasonMapper;
import com.zheng.mall.model.OmsOrderReturnReason;
import com.zheng.mall.model.OmsOrderReturnReasonExample;
import com.zheng.mall.service.OmsOrderReturnReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单原因管理Service
 * @author zheng
 */
@Service
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {
    @Autowired
    private OmsOrderReturnReasonMapper orderReturnReasonMapper;

    /**
     * 添加订单原因
     *
     * @param returnReason
     */
    @Override
    public int create(OmsOrderReturnReason returnReason) {
        returnReason.setCreateTime(new Date());
        return orderReturnReasonMapper.insert(returnReason);
    }

    /**
     * 修改退货原因
     *
     * @param id
     * @param returnReason
     */
    @Override
    public int update(Long id, OmsOrderReturnReason returnReason) {
        returnReason.setId(id);
        return orderReturnReasonMapper.updateByPrimaryKey(returnReason);
    }

    /**
     * 批量删除退货原因
     *
     * @param ids
     */
    @Override
    public int delete(List<Long> ids) {
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return orderReturnReasonMapper.deleteByExample(example);
    }

    /**
     * 分页获取退货原因
     *
     * @param pageSize
     * @param pageNum
     */
    @Override
    public List<OmsOrderReturnReason> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.setOrderByClause("sort desc");
        return orderReturnReasonMapper.selectByExample(example);
    }

    /**
     * 批量修改退货原因状态
     *
     * @param ids
     * @param status
     */
    @Override
    public int updateStatus(List<Long> ids, Integer status) {
        if (!status.equals(0) && !status.equals(1)) {
            return 0;
        }
        OmsOrderReturnReason orderReturnReason = new OmsOrderReturnReason();
        orderReturnReason.setStatus(status);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return orderReturnReasonMapper.updateByExample(orderReturnReason, example);
    }

    /**
     * 获取单个退货原因详情信息
     *
     * @param id
     */
    @Override
    public OmsOrderReturnReason getItem(Long id) {
        return orderReturnReasonMapper.selectByPrimaryKey(id);
    }
}
